<template>
	<view class="edit">
				<view class="add_item">
					<view>产品名称</view>
					<input type="text" v-model="upJson.title" placeholder="请输入产品名称，必填"/>
				</view>
				
				
				<view class="add_image">
					<view>上传照片</view>
					<view class="add_image_box">
						<block v-if="imgList.length > 0 ">
							<view class="image_box" :key="index">
								<image src="/static/images/del_btn.png" class="del_btn" mode="aspectFill" @click.stop="delImg(0)"></image>
								<image :src="imgList[0]" class="add_header" @tap="lookImg(0)" mode="aspectFill"></image>
							</view>
						</block>
						<view class="image_box add_inner_box">
							<image src="/static/images/img_add.png" mode="aspectFill" @tap="addImage()"></image>
						</view>
					</view>
				</view>

				<view class="add_item">
					<view>产品编号</view>
					<input type="text" v-model="upJson.number" placeholder="请输入产品编号，必填"/>
				</view>
				<view class="add_item">
					<view>产品类型</view>
					<picker mode="selector" :range="typeList" :data-name="'typeIndex'" @change="getPickerValue" class="add_picker">
						<text>{{typeList[typeIndex]}}</text>
						<image src="/static/images/right.png" class="right"></image>
					</picker>
				</view>
				<view class="add_item">
					<view>产品型号</view>
					<input type="text" v-model="upJson.module" placeholder="请输入产品型号"/>
				</view>
				<view class="add_item">
					<view>产品单位</view>
					<input type="text" v-model="upJson.unit" placeholder="请输入产品单位"/>
				</view>
				<view class="add_item">
					<view>产品规格</view>
					<input type="text" v-model="upJson.specifications" placeholder="请输入产品规格"/>
				</view>
				<view class="add_item">
					<view>销售价格</view>
					<input type="number" v-model="upJson.price" placeholder="请输入销售价格，必填"/>
				</view>
				<view class="add_item">
					<view>成本价格</view>
					<input type="number" v-model="upJson.cost_price" placeholder="请输入成本价格，必填"/>
				</view>
				<view class="add_item">
					<view>结算价格</view>
					<input type="number" v-model="upJson.settlement" placeholder="请输入结算价格，必填"/>
				</view>

				<view class="add_item">
					<view>调整状态</view>
					<input type="text" v-model="upJson.mode" placeholder="请输入调整状态"/>
				</view>

				<view class="add_item">
					<view>总库存预警数量</view>
					<input type="number" v-model="upJson.min_stock" placeholder="请输入总库存预警数量"/>
				</view>

				<view class="add_item">
					<view>分库存预警数量</view>
					<input type="number" v-model="upJson.point_min_stock" placeholder="请输入分库存预警数量"/>
				</view>

				<view class="add_item" @click.stop="openSearch('czr_user')">
					<view>操作人</view>
					<input type="text" v-model="czr" disabled placeholder="请选择操作人" />
					<image src="/static/images/right.png" class="right"></image>
				</view>

				<view class="add_item">
					<view>排序</view>
					<input type="number" v-model="upJson.sort" placeholder="请输入排序，必填"/>
				</view>

				<view class="add_textarea">
					<view>备注</view>
					<textarea v-model="upJson.desc" placeholder="请输入备注" />
				</view>
			 
			
			<view class="save" @tap="submit()">确认</view>
	</view>
</template>

<script>
	import {
		deepClone
	} from '@/static/js/index.js'
	export default{
		data(){
			return{
				typeList:[],   //类型集合
				typeIndex: 0,
				upJson:{
					title:'',
					number:'',
					file:'', // 文件
					type:'',   // 类型
					module:'', // 型号
					unit:'', // 单位
					specifications:'', // 规格
					price:'', // 销售价格
					cost_price:'', // 成本价格
					settlement:'', // 结算价格
					mode:'', // 调整状态
					min_stock:20, //总库存预警数量
					point_min_stock:20, // 分库存预警数量
					action_user_id:0, // 操作人id
					sort:'',  //排序
					desc:'', // 备注
					user_id:0 
				},
				czr:'',  // 操作人
				imgList:[],
			}
		},
		onLoad(options) {

			this.productType();
			this.upJson.user_id = this.$store.getters.userInfo.id;
			
		},
		onShow() {
			this.setBaseNews();
		},
		methods:{
			//设置基础信息
			setBaseNews(){
				let obj=deepClone(this.$store.getters['parameters/searchObj']);
				console.log("返回的数据----》》",obj);
				 if(obj.key=="czr_user"){
					this.czr=obj.data.username;
					this.upJson.action_user_id=obj.data.id;
				}
				this.$store.dispatch('parameters/deleteObj');
			},
			//打开搜索列表
			openSearch(key){
				uni.navigateTo({
					url:'/pages/searchList/searchList?searchKey='+key
				})
			},
			addImage(){
				uni.chooseImage({
					success:res => {
						//console.log("文件---》》》",res.tempFilePaths);
						if(this.imgList.length == 0){
							this.imgList.push.apply(this.imgList,res.tempFilePaths)
						}else{
							this.imgList[0] =  res.tempFilePaths[0];
							
						}
					}
				})
				//console.log("进入----》》》》",this.imgList[0]);
			},
			lookImg(index){
				let that=this;
				uni.previewImage({
					urls: that.imgList,
					current:index,
				})
				
			},
			delImg(index){
				uni.showModal({
					title:"温馨提示",
					content:"是否删除该图片?",
					success:res =>{
						if (res.confirm) {
						    this.imgList.splice(index,1)
						}
					}
				})
			},
			productType(){
				this.$api.getAccessoriesType({}).then(res =>{
					this.typeList = res

					this.upJson.type = this.typeList[this.typeIndex];
				})
			},
			getPickerValue(e){
				this[e.currentTarget.dataset.name] = e.detail.value;

				this.upJson.type = this.typeList[e.detail.value];
			},
			submit(){

				let upJson = this.upJson;

				if(upJson.title==''||upJson.title.length==0){
					uni.showToast({title:"请输入产品名称",icon:"none"})
				}else if(upJson.number==''||upJson.number.length==0){
					uni.showToast({title:"请输入产品编号",icon:"none"})
				}else if(upJson.price==''||upJson.price.length==0){
					uni.showToast({title:"请输入销售价格",icon:"none"})
				}else if(upJson.cost_price==''||upJson.cost_price.length==0){
					uni.showToast({title:"请输入成本价格",icon:"none"})
				}else if(upJson.settlement==''||upJson.settlement.length==0){
					uni.showToast({title:"请输入结算价格",icon:"none"})
				}else if(upJson.sort==''||upJson.sort.length==0){
					uni.showToast({title:"请输入排序",icon:"none"})
				}else if(upJson.action_user_id==0){
					uni.showToast({title:"请选择操作人",icon:"none"})
				}else{
					uni.showToast({title:"功能正在研发中！",icon:"none"})

					// 需要验证文件位置是否为空
					this.$api.uploadFile(this.imgList).then(res =>{
						upJson.file = res[0] // 设置文件位置
						// 修改订单接口
						
						this.$api.addProduct(upJson)

					})

				}

			}
		}
	}
</script>

<style>
	.edit{width: 100%;background: #FFFFFF;padding-bottom: 50upx;}
</style>
